package com.ist.acceleratedrawdemo

import android.graphics.PointF
import android.graphics.RectF
import android.view.MotionEvent

/**
 * Created by Cookies on 2020/12/26
 */
class Shape : Line() {
    private var downP: PointF? = null
    private var currP: PointF? = null
    private var rect: RectF? = null
    fun onDown(event: MotionEvent) {
        for (i in 0 until event.pointerCount) {
            if (actionPointId == event.getPointerId(i)) {
                downP = PointF(event.getX(i), event.getY(i))
                currP = PointF(event.getX(i), event.getY(i))
                rect = RectF(downP!!.x, downP!!.y, downP!!.x, downP!!.y)
                break
            }
        }
    }

    fun onMove(event: MotionEvent) {
        for (i in 0 until event.pointerCount) {
            if (actionPointId == event.getPointerId(i)) {
                currP!!.x = event.getX(i)
                currP!!.y = event.getY(i)
                rect!![downP!!.x, downP!!.y, downP!!.x] = downP!!.y
                rect!!.union(currP!!.x, currP!!.y)
                break
            }
        }
    }

    fun onUp(event: MotionEvent?) {
        actionPointId = -1
    }

    val rectF: RectF
        get() = RectF(rect)
    val vagueRectF: RectF
        get() {
            val strokeWidth = paint.strokeWidth
            val r = RectF(rect)
            r.left -= strokeWidth / 2
            r.top -= strokeWidth / 2
            r.right += strokeWidth / 2
            r.bottom += strokeWidth / 2
            return r
        }
}
